<?php

/**
 * Description
 *
 * PHP version 7
 *
 * @category Custom
 * @package  Custom
 * @author   ryu <mo5467@126.com>
 * @date     2020-04-22 23:17:52
 * @lastTime 2020-05-01 22:57:23
 * @license  https://opensource.org/licenses/MIT MIT License
 * @link     none
 */

namespace Test\Unit;

use Design\Creational\AbstractFactory\CsvWriter;
use Design\Creational\AbstractFactory\JsonWriter;
use Design\Creational\AbstractFactory\WriterFactory;

use Design\Creational\AbstractFactory\UnixWriterFactory;
use Design\Creational\AbstractFactory\WinWriterFactory;

class AbstractFactoryTest extends \Test\BaseTest
{


    public function factoryProvider()
    {
        return [
            [new WinWriterFactory()],
            [new UnixWriterFactory()],
        ];

    }//end factoryProvider()


    /**
     * @dataProvider factoryProvider
     */
    public function testFactory(WriterFactory $factory)
    {
        $this->assertInstanceOf(CsvWriter::class, $factory->createCsvWriter());
        $this->assertInstanceOf(JsonWriter::class, $factory->createJsonWriter());

    }//end testFactory()


    public function testWrite()
    {
        $arr = [
            1,
            2,
        ];

        $winWriterFactory = new WinWriterFactory();
        $this->assertSame(json_encode($arr, JSON_PRETTY_PRINT), $winWriterFactory->createJsonWriter()->write($arr));
        $this->assertSame("1,2\r\n", $winWriterFactory->createCsvWriter()->write($arr));

        $unixWriterFactory = new UnixWriterFactory();
        $this->assertSame(json_encode($arr, 0), $unixWriterFactory->createJsonWriter()->write($arr));
        $this->assertSame(
            json_encode($arr, JSON_PRETTY_PRINT),
            $unixWriterFactory->createJsonWriter()->write($arr, true)
        );
        $this->assertSame("1,2\n", $unixWriterFactory->createCsvWriter()->write($arr));

    }//end testWrite()


}//end class
